CREATE  trigger tfb_cashflow_ad on dbo.tfb_cashflow 
after delete  

as   


begin   

  set nocount on
  declare @nyear integer   
  declare @nmonth integer   
  declare @nflowid integer ,@filid int   
  declare @debitamt decimal(18,4)   
  declare @creditamt decimal(18,4)   
  
  declare @voucherid integer

  select @voucherid=voucherid,@nflowid=flowid,@debitamt=debit,@creditamt= credit ,@filid = filid from deleted

  if (@voucherid>0)    
  begin   
    select  @nyear=cast(substring(c.vouyearmonth,1,4) as integer),   
      @nmonth=cast(substring(c.vouyearmonth,5,6) as integer)   
      from tfb_voucher c,deleted   
      where c.voucherid=deleted.voucherid  and c.filid=deleted.filid  
     
    if ((@nyear is not null) and (@nmonth is not null))    
    begin      
    update tfb_cashflowinit set debit = debit - @debitamt, credit = credit - @creditamt                               
    where flowid = @nflowid   
      and nyear = @nyear and  nmonth = @nmonth and filid=@filid   
    end      
  end   
end


